package service;

import model.User;
import util.LogUtil;

import java.util.*;

public class UserService {
    private List<User> users = new ArrayList<>();

    public void initAdmin() {
        users.add(new User("1", "superadmin", "160041", "admin", "12345678901"));
    }

    public User login(String username, String password) {
        for (User u : users) {
            if (u.getUsername().equals(username) && u.getPassword().equals(password)) {
                return u;
            }
        }
        return null;
    }

    public void passwordMenu(Scanner sc, User user) {
        System.out.println("1. 修改自身密码 2. 重置他人密码");
        int op = sc.nextInt();
        if (op == 1) {
            System.out.print("新密码: ");
            String newPwd = sc.next();
            user.setPassword(newPwd);
            LogUtil.log("修改密码", user.getUsername());
            System.out.println("修改成功！");
        } else if (op == 2) {
            System.out.print("输入要重置的用户名: ");
            String uname = sc.next();
            for (User u : users) {
                if (u.getUsername().equals(uname) && !u.getType().equals("admin")) {
                    u.setPassword("123456");
                    LogUtil.log("重置密码", user.getUsername());
                    System.out.println("重置成功！");
                    return;
                }
            }
            System.out.println("未找到用户或不能重置管理员密码！");
        }
    }

    public void userManageMenu(Scanner sc) {
        System.out.println("1. 列出所有用户 2. 添加用户 3. 删除用户 4. 查询用户 5. 修改用户 6. 返回");
        int op = sc.nextInt();
        switch (op) {
            case 1:
                for (User u : users) {
                    System.out.println(u.getId() + " " + u.getUsername() + " " + u.getType() + " " + u.getPhone());
                }
                break;
            case 2:
                System.out.print("输入用户名: ");
                String uname = sc.next();
                System.out.print("输入类型(admin/manager/front): ");
                String type = sc.next();
                System.out.print("输入手机号: ");
                String phone = sc.next();
                String id = String.valueOf(users.size() + 1);
                users.add(new User(id, uname, "123456", type, phone));
                LogUtil.log("添加用户", uname);
                System.out.println("添加成功，初始密码123456");
                break;
            case 3:
                System.out.print("输入要删除的用户名: ");
                String delName = sc.next();
                if (delName.equals("admin")) {
                    System.out.println("不能删除admin账号！");
                    return;
                }
                users.removeIf(u -> u.getUsername().equals(delName));
                LogUtil.log("删除用户", delName);
                System.out.println("删除成功！");
                break;
            case 4:
                System.out.print("输入用户名: ");
                String qname = sc.next();
                for (User u : users) {
                    if (u.getUsername().equals(qname)) {
                        System.out.println(u.getId() + " " + u.getUsername() + " " + u.getType() + " " + u.getPhone());
                        return;
                    }
                }
                System.out.println("未找到用户！");
                break;
            case 5:
                System.out.print("输入用户名: ");
                String mname = sc.next();
                for (User u : users) {
                    if (u.getUsername().equals(mname)) {
                        System.out.print("新类型(manager/front): ");
                        String ntype = sc.next();
                        System.out.print("新手机号: ");
                        String nphone = sc.next();
                        u.setType(ntype);
                        u.setPhone(nphone);
                        LogUtil.log("修改用户", mname);
                        System.out.println("修改成功！");
                        return;
                    }
                }
                System.out.println("未找到用户！");
                break;
            case 6:
                return;
        }
    }
}